package cn.kawa.searchMsg.controller;

import cn.kawa.basic.exception.BusinessException;
import cn.kawa.basic.util.LoginContext;

import cn.kawa.basic.util.JsonResult;
import cn.kawa.basic.util.PageList;
import cn.kawa.searchMsg.domain.SearchMasterMsg;
import cn.kawa.searchMsg.dto.AcceptDto;
import cn.kawa.searchMsg.query.SearchMasterMsgQuery;
import cn.kawa.searchMsg.service.ISearchMasterMsgService;
import cn.kawa.user.domain.LoginInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：用于商户接单
     *
     * @param
     * @return
     */
    @PostMapping("/accept")
    @ApiOperation(value = "用于商户接单")
    public JsonResult accept(@RequestBody AcceptDto acceptDto, HttpServletRequest request) {
        LoginInfo loginUser = LoginContext.getLoginUser(request);
        try {
            searchMasterMsgService.accept(acceptDto, loginUser.getId());
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("接单失败");
        }
    }

    /**
     * 接口：用于商户拒单
     *
     * @param
     * @return
     */
    @GetMapping("/reject/{smmId}")
    @ApiOperation(value = "寻主信息")
    public JsonResult reject(@PathVariable("smmId") Long smmId) {//,HttpServletRequest request
        //LoginInfo loginUser = LoginContext.getLoginUser(request);
        try {
            searchMasterMsgService.reject(smmId);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("拒单失败");
        }
    }


    /**
     * 接口：分页查询或高级查询  重构接口，用于展示 待审核0 或者 待处理state:1信息
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        LoginInfo loginUser = LoginContext.getLoginUser(request);
        Long id = loginUser.getId();
        return searchMasterMsgService.queryPageMsg(query, id);
    }


    /**
     * 接口：用于保存寻主信息
     *
     * @param searchMasterMsg 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/publish")
    @ApiOperation(value = "寻主信息")
    public JsonResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        try {
            LoginInfo loginUser = LoginContext.getLoginUser(request);
            searchMasterMsgService.publish(searchMasterMsg, loginUser.getId());
            return new JsonResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常");
        }
    }

    /**
     * 接口：添加或修改
     *
     * @param searchMasterMsg 传递的实体
     * @return JsonResult 响应给前端
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return JsonResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public JsonResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.delete(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return JsonResult 响应给前端
     */
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids) {
        try {
            searchMasterMsgService.patchDel(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id") Long id) {
        return searchMasterMsgService.findById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<SearchMasterMsg> findAll() {
        return searchMasterMsgService.findAll();
    }

}